<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="问题描述" prop="problemDesc">
        <el-input
          v-model="queryParams.problemDesc"
          placeholder="请输入参数"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button  @click="addData">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="参数">
        <template #default="scope">
          <el-input
            v-if="scope.$index === 0 && addFlag"
            v-model="tableForm.code"
            placeholder="请输入参数"
            clearable
          />
          <span v-else>{{scope.row.code}}</span>
        </template>
      </el-table-column>
      <el-table-column label="参数说明">
        <template #default="scope">
          <el-input
            v-if="scope.$index === 0 && addFlag"
            v-model="tableForm.txt"
            placeholder="请输入参数"
            clearable
          />
          <span v-else>{{scope.row.txt}}</span>
        </template>
      </el-table-column>
      <el-table-column label="参数类型">
        <template #default="scope">
          <el-select
            v-if="scope.$index === 0 && addFlag"
            v-model="tableForm.type"
            placeholder="请选择"
            clearable
          >
            <el-option key="" label="请选择" value="" />
            <el-option key="1" label="输入型参数" value="1" />
            <el-option key="2" label="配置型参数" value="2" />
            <el-option key="3" label="计算型参数" value="3" />
          </el-select>
          <span v-else>{{scope.row.type}}</span>
        </template>
      </el-table-column>
      <el-table-column label="参数相关性">
        <template #default="scope">
          <el-select 
            v-if="scope.$index === 0 && addFlag"
            v-model="tableForm.valueType"
            placeholder="请选择"
            clearable
          >
              <el-option v-for="item in valueTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <span v-else>{{scope.row.valueType}}</span>
        </template>
      </el-table-column>
      <el-table-column label="参数码表">
        <template #default="scope">
          <el-input
            v-if="scope.$index === 0 && addFlag"
            v-model="tableForm.sourceTable"
            placeholder="请输入参数"
            clearable
          />
          <span v-else>{{scope.row.sourceTable}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-tooltip content="保存" placement="top" v-if="scope.$index === 0 && addFlag">
            <el-button link type="primary" icon="DocumentChecked" @click="handleSave()"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" v-if="scope.$index === 0 && addFlag">
            <el-button link type="danger" icon="CircleClose" @click="handleClear(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup name="Problem">
// import { problemQuery, problemSave } from '@/api/safeguard/index.js';
const queryParams = ref({
  problemDesc: ''
})
const addFlag = ref(false)
const showSearch = ref(true);
const loading = ref(false);
const tableForm = ref({
  listId: '',
  txt: '', // 参数
  code: '', // 参数编码
  type: '', // 参数类型
  valueType: '', // 参数相关性
  sourceTable: '', // 参数码表
})
const tableData = ref([]);

function getList () {
  loading.value = true;
  problemQuery('111').then(res => {
    tableData.value = res.data || []
  }).finally(() => {
    loading.value = false;
  })
}

function handleQuery() {
  getList();
}


 /** 添加 */
 function addData () {
  if(addFlag.value) return
  tableData.value.unshift({...tableForm.value})
  addFlag.value = true
}

function handleSave() {  
  // 保存成功刷新数据
  parameterAdd({...tableForm.value}).then(() => {
    addFlag.value = false
    getList();
  })
}
function handleClear (row) {
  tableForm.value = {
    listId: '',
    txt: '', // 参数
    code: '', // 参数编码
    type: '', // 参数类型
    valueType: '', // 参数相关性
    sourceTable: '', // 参数码表
  }
  addFlag.value = false
  tableData.value.shift()
}

// handleQuery()
</script>
